﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using RM2012Server.Controller;
using RM2012Server.Business;
using RM2012Server.Model;

namespace RM2012Server.View
{
    public partial class frmServe2 : Office2007Form
    {
        #region Fields
        InvoiceCtrl m_InvoiceCtrl = new InvoiceCtrl();
        InvoiceDetailCtrl m_DetailCtrl = new InvoiceDetailCtrl();
        TablesCtrl m_TablesCtrl = new TablesCtrl();
        CustomerCtrl m_CustomerCtrl = new CustomerCtrl();
        UnitCtrl m_UnitCtrl = new UnitCtrl();
        MenuCtrl m_MenuCtrl = new MenuCtrl();
        BookingCtrl m_BookingCtrl = new BookingCtrl();
        CategoryCtrl m_CategoryCtrl = new CategoryCtrl();
        #endregion

        #region Constructor
        public frmServe2()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void frmServe2_Load(object sender, EventArgs e)
        {
            m_TablesCtrl.LoadToComboBox(cmbTablesId);
            m_TablesCtrl.LoadToComboBox(cmbServerTables);
            m_CustomerCtrl.LoadToComboBox(cmbCusId);
            LoadCategory();
            m_BookingCtrl.LoadToComboBoxUnCompleted(cmbServerBooking);
            m_CustomerCtrl.LoadToDataGridViewComboBoxColumn(colCusId);
            m_TablesCtrl.LoadToDataGridViewComboBoxColumn(colTablesId);
            m_DetailCtrl.LoadToDataGridViewComboBoxColumnState(colState);
            m_InvoiceCtrl.LoadToDataGridView(dgvInvoice);
            LoadGridDataAtStart();
        }
        #endregion

        #region Public Function
        public void BindDataInvoice(int rowIndex)
        {
            if (rowIndex >= 0)
            {
                String invoiceId = dgvInvoice.Rows[rowIndex].Cells["colInvoiceId"].Value.ToString();
                String cusId = dgvInvoice.Rows[rowIndex].Cells["colCusId"].Value.ToString();
                String tablesId = dgvInvoice.Rows[rowIndex].Cells["colTablesId"].Value.ToString();
                DateTime invoiceDate = Convert.ToDateTime(dgvInvoice.Rows[rowIndex].Cells["colInvoiceDate"].Value);
                String total = dgvInvoice.Rows[rowIndex].Cells["colTotal"].Value.ToString();
                double totalDouble = Convert.ToDouble(total);
                String totalStr = Convert.ToString(totalDouble);
                String paid = dgvInvoice.Rows[rowIndex].Cells["colPaid"].Value.ToString();
                double paidDouble = Convert.ToDouble(paid);
                String paidStr = Convert.ToString(paidDouble);
                bool isPaid = Convert.ToBoolean(dgvInvoice.Rows[rowIndex].Cells["colIsPaid"].Value);
                bool isDeleted = Convert.ToBoolean(dgvInvoice.Rows[rowIndex].Cells["colIsDeleted"].Value);
                String discountPer = dgvInvoice.Rows[rowIndex].Cells["colDiscountPer"].Value.ToString();
                double discountPerDouble = Convert.ToDouble(discountPer);
                String discountPerStr = Convert.ToString(discountPerDouble);
                String remark = dgvInvoice.Rows[rowIndex].Cells["colRemark"].Value.ToString();
                double paidReally = totalDouble - totalDouble * discountPerDouble / 100;
                String paidReallyStr = Convert.ToString(paidReally);
                double debt = paidReally - paidDouble;
                String debtStr = "";
                if (debt < 0)
                {
                    debtStr = "0";
                }
                else
                {
                    debtStr = Convert.ToString(debt);
                }

                //Bind Data to Controls
                txtInvoiceId.Text = invoiceId;
                dtInvoiceDate.Value = invoiceDate;
                cmbTablesId.SelectedValue = tablesId;
                cmbCusId.SelectedValue = cusId;
                txtDiscountPer.Text = discountPerStr;
                txtRemark.Text = remark;
                txtTotal.Text = totalStr;
                txtPaid.Text = paidStr;
                txtDebt.Text = debtStr;
                txtPaidReally.Text = paidReallyStr;

                //If invoice is paid
                if (isPaid)
                {
                    btnPay.Enabled = false;
                    btnAddMenu.Enabled = false;
                    btnSave.Enabled = false;
                    btnDelete.Enabled = false;
                }
                else
                {
                    btnPay.Enabled = true;
                    btnAddMenu.Enabled = true;
                    btnSave.Enabled = true;
                    btnDelete.Enabled = true;
                }

                //Bind Data to Grid Detail
                RefreshGridDetail(invoiceId);
                groupMenu.Text = "Danh sách món phục vụ - " + "Hóa đơn <font color='red'>" + invoiceId + "</font>";
            }
        }

        public void BindDataInvoiceDetail(int rowIndex)
        {
            if (rowIndex >= 0)
            {
                String invoiceId = dgvInvoiceDetail.Rows[rowIndex].Cells["colInvoiceId2"].Value.ToString();
                String menuId = dgvInvoiceDetail.Rows[rowIndex].Cells["colMenuId"].Value.ToString();
                String unitId = m_MenuCtrl.GetUnitId(menuId);
                bool state = Convert.ToBoolean(dgvInvoiceDetail.Rows[rowIndex].Cells["colState"].Value);
                String quantity = dgvInvoiceDetail.Rows[rowIndex].Cells["colQuantity"].Value.ToString();
                double quantityDouble = Convert.ToDouble(quantity);
                String quantityStr = Convert.ToString(quantityDouble);
                int iQuantity = Convert.ToInt32(quantityDouble);
                String price = dgvInvoiceDetail.Rows[rowIndex].Cells["colPrice"].Value.ToString();
                double priceDouble = Convert.ToDouble(price);
                String priceStr = Convert.ToString(priceDouble);
                bool isDeleted = Convert.ToBoolean(dgvInvoiceDetail.Rows[rowIndex].Cells["colIsDeleted2"].Value);
                DataTable dtUnit = m_UnitCtrl.GetById(unitId);
                DataTable dtMenu = m_MenuCtrl.GetById(menuId);
                String unitName = "";
                String cateId = "";
                if (dtUnit.Rows.Count > 0)
                {
                    unitName = dtUnit.Rows[0]["UnitName"].ToString();
                }
                if (dtMenu.Rows.Count > 0)
                {
                    cateId = dtMenu.Rows[0]["CateId"].ToString();
                }
                //Bind Data to Controls
                lblUnit.Text = unitName;
                iiQuantity.Value = iQuantity;
                txtPrice.Text = priceStr;
                chkState.Checked = state;
                txtMenuId.Text = menuId;
                SetSelectedForCategoryListView(cateId);
                SetSelectedForMenuListView(menuId);
            }
        }

        public String AddInvoice(String cusId)
        {
            DateTime dateNow = DateTime.Now;
            String _year = dateNow.Year.ToString();
            String _month = dateNow.Month.ToString();
            if (dateNow.Month < 10)
            {
                _month = "0" + dateNow.Month.ToString();
            }
            String _day = dateNow.Day.ToString();
            if (dateNow.Day < 10)
            {
                _day = "0" + dateNow.Day.ToString();
            }
            String _hour = dateNow.Hour.ToString();
            if (dateNow.Hour < 10)
            {
                _hour = "0" + dateNow.Hour.ToString();
            }
            String _minute = dateNow.Minute.ToString();
            if (dateNow.Minute < 10)
            {
                _minute = "0" + dateNow.Minute.ToString();
            }
            String _second = dateNow.Second.ToString();
            if (dateNow.Second < 10)
            {
                _second = "0" + dateNow.Second.ToString();
            }
            String invoiceId = _year + _month + _day + _hour + _minute + _second;
            InvoiceInfo info = new InvoiceInfo();
            info.InvoiceId = invoiceId;
            info.InvoiceDate = dateNow;
            info.CusId = cusId;
            info.TablesId = cmbServerTables.SelectedValue.ToString();
            info.Total = 0;
            info.Paid = 0;
            info.IsDeleted = false;
            info.IsPaid = false;
            info.DiscountPer = 0;
            info.Remark = "";
            new InvoiceCtrl().Add(info);
            return info.InvoiceId;
        }

        public void LoadGridDataAtStart()
        {
            if (dgvInvoice.Rows.Count > 0)
            {
                BindDataInvoice(0);
                if (dgvInvoiceDetail.Rows.Count > 0)
                {
                    BindDataInvoiceDetail(0);
                }
            }
            SetSelectedForCategoryListView("");
            SetSelectedForMenuListView("");
        }

        public double GetTotal(String invoiceId)
        {
            double total = 0.0;
            DataTable dtDetail = m_DetailCtrl.GetByInvoiceId(invoiceId);
            if (dtDetail.Rows.Count > 0)
            {
                for (int i = 0; i < dtDetail.Rows.Count; i++)
                {
                    total = total + Convert.ToDouble(dtDetail.Rows[i]["Price"].ToString()) * Convert.ToDouble(dtDetail.Rows[i]["Quantity"].ToString());
                }
            }
            return total;
        }

        public void RefreshGridInvoice()
        {
            DataTable dtSource = m_InvoiceCtrl.GetAll();
            dgvInvoice.DataSource = dtSource;
        }

        public void RefreshGridDetail(String invoiceId)
        {
            DataTable dtSource = m_DetailCtrl.GetFullByInvoiceId(invoiceId);
            dgvInvoiceDetail.DataSource = dtSource;
        }

        public void LoadCategory()
        {
            lvCategory.Items.Clear();
            DataTable dtCategory = m_CategoryCtrl.GetAll();
            if (dtCategory.Rows.Count > 0)
            {
                for (int i = 0; i < dtCategory.Rows.Count; i++)
                {
                    ListViewItem item = new ListViewItem(dtCategory.Rows[i]["CateName"].ToString());
                    ListViewItem.ListViewSubItem subId = new ListViewItem.ListViewSubItem(item, dtCategory.Rows[i]["CateId"].ToString());
                    item.SubItems.Add(subId);
                    lvCategory.Items.Add(item);
                }
            }
        }

        private void LoadMenuByCate(String cateId)
        {
            lvMenu.Items.Clear();
            DataTable dtMenuSource = m_MenuCtrl.GetByCate(cateId);
            if (dtMenuSource.Rows.Count > 0)
            {
                for (int i = 0; i < dtMenuSource.Rows.Count; i++)
                {
                    ListViewItem item = new ListViewItem(dtMenuSource.Rows[i]["MenuName"].ToString());
                    ListViewItem.ListViewSubItem subPrice = new ListViewItem.ListViewSubItem(item, dtMenuSource.Rows[i]["Price"].ToString());
                    ListViewItem.ListViewSubItem subId = new ListViewItem.ListViewSubItem(item, dtMenuSource.Rows[i]["MenuId"].ToString());
                    item.SubItems.Add(subPrice);
                    item.SubItems.Add(subId);
                    lvMenu.Items.Add(item);
                }
                lvMenu.Items[0].Selected = true;
            }
        }

        public void SetSelectedForMenuListView(String menuId)
        {
            lvMenu.SelectedItems.Clear();
            if (menuId == "")
            {
                if (lvMenu.Items.Count > 0)
                {
                    lvMenu.Items[0].Selected = true;
                }
            } 
            else
            {
                if (lvMenu.Items.Count > 0)
                {
                    foreach (ListViewItem itemMenu in lvMenu.Items)
                    {
                        ListViewItem.ListViewSubItem subId = itemMenu.SubItems[2];
                        if (subId.Text == menuId)
                        {
                            itemMenu.Selected = true;
                        }
                    }
                }
            }
        }

        public void SetSelectedForCategoryListView(String cateId)
        {
            lvCategory.SelectedItems.Clear();
            if (cateId == "")
            {
                if (lvCategory.Items.Count > 0)
                {
                    lvCategory.Items[0].Selected = true;
                }
            }
            else
            {
                if (lvCategory.Items.Count > 0)
                {
                    foreach(ListViewItem itemCate in lvCategory.Items)
                    {
                        ListViewItem.ListViewSubItem subId = itemCate.SubItems[1];
                        if (subId.Text == cateId)
                        {
                            itemCate.Selected = true;
                        }
                    }
                }
            }
        }

        public int SetSelectedForInvoiceGridView(String invoiceId)
        {
            int rowIndex = 0;
            if (dgvInvoice.Rows.Count > 0)
            {
                for (int i = 0; i < dgvInvoice.Rows.Count; i++)
                {
                    if (dgvInvoice.Rows[i].Cells["colInvoiceId"].Value.ToString().Equals(invoiceId))
                    {
                        rowIndex = i;
                        dgvInvoice.Rows[i].Selected = true;
                    }
                }
            }
            return rowIndex;
        }

        public String AddCustomerNew()
        {
            CustomerInfo info = new CustomerInfo();
            info.CusId = GetID.LastCustomerID();
            if (txtFullNameNew.Text == "" || txtFullNameNew.Text == null)
            {
                info.FullName = "Khách lẻ";
            }
            else
            {
                info.FullName = txtFullNameNew.Text;
            }
            info.Address = "";
            info.Sex = true;
            info.DiscountPer = 0;
            info.Debt = 0;
            info.Phone = "";
            info.Email = "";
            info.State = "";
            info.Remark = "";
            new CustomerCtrl().Add(info);
            new CustomerCtrl().UpdateSex(info.CusId);
            return info.CusId;
        }

        public bool IsExistedMenu(String invoiceId, String menuId)
        {
            DataTable dtCount = m_DetailCtrl.GetByMenuAndInvoice(invoiceId, menuId);
            if (dtCount.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region Event Handles
        private void dgvInvoice_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //Get Data from Grid
            int rowIndex = e.RowIndex;
            BindDataInvoice(rowIndex);
        }

        private void dgvInvoiceDetail_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            BindDataInvoiceDetail(rowIndex);
        }

        private void chkIsNewCustomer_CheckedChanged(object sender, EventArgs e)
        {
            if (chkIsNewCustomer.Checked == true)
            {
                lblServerBooking.Text = "Tên : ";
                cmbServerBooking.Visible = false;
                txtFullNameNew.Visible = true;
            } 
            else
            {
                lblServerBooking.Text = "Đặt trước";
                cmbServerBooking.Visible = true;
                txtFullNameNew.Visible = false;
            }
        }

        private void btnServer_Click(object sender, EventArgs e)
        {
            try
            {
                //Add Customer if IsNew and After Add Invoice for Customer
                String cusId = "";
                String invoiceId = "";
                if (chkIsNewCustomer.Checked == true)
                {
                    cusId = AddCustomerNew();
                    invoiceId = AddInvoice(cusId);
                } 
                else
                {
                    cusId = cmbServerBooking.SelectedValue.ToString();
                    invoiceId = AddInvoice(cusId);
                }
                //Load Data Again
                m_CustomerCtrl.LoadToDataGridViewComboBoxColumn(colCusId);
                m_TablesCtrl.LoadToDataGridViewComboBoxColumn(colTablesId);
                m_DetailCtrl.LoadToDataGridViewComboBoxColumnState(colState);
                m_InvoiceCtrl.LoadToDataGridView(dgvInvoice);
                dgvInvoice.Rows[dgvInvoice.Rows.Count - 1].Selected = true;
            }
            catch
            {
                //MessageBox.Show(ex.Message);
            }
        }

        private void dgvInvoice_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show(e.Exception.Message);
        }

        private void dgvInvoiceDetail_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show(e.Exception.Message + "-" + e.Exception.Source);
        }

        private void btnToday_Click(object sender, EventArgs e)
        {
            m_CustomerCtrl.LoadToDataGridViewComboBoxColumn(colCusId);
            m_TablesCtrl.LoadToDataGridViewComboBoxColumn(colTablesId);
            m_DetailCtrl.LoadToDataGridViewComboBoxColumnState(colState);
            m_InvoiceCtrl.LoadToDataGridViewToday(dgvInvoice);
        }

        private void btnAllInvoice_Click(object sender, EventArgs e)
        {
            m_CustomerCtrl.LoadToDataGridViewComboBoxColumn(colCusId);
            m_TablesCtrl.LoadToDataGridViewComboBoxColumn(colTablesId);
            m_DetailCtrl.LoadToDataGridViewComboBoxColumnState(colState);
            m_InvoiceCtrl.LoadToDataGridView(dgvInvoice);
        }

        private void btnPay_Click(object sender, EventArgs e)
        {
            try
            {
                double mustPaid = Convert.ToDouble(txtPaidReally.Text);
                InvoiceInfo info = new InvoiceInfo();
                info.InvoiceId = txtInvoiceId.Text;
                info.InvoiceDate = dtInvoiceDate.Value;
                info.CusId = cmbCusId.SelectedValue.ToString();
                info.TablesId = cmbTablesId.SelectedValue.ToString();
                info.Total = Convert.ToDouble(txtTotal.Text);
                try
                {
                    info.DiscountPer = Convert.ToDouble(txtDiscountPer.Text);
                }
                catch
                {
                    info.DiscountPer = 0.0;
                }
                try
                {
                    info.Paid = Convert.ToDouble(txtPaid.Text);
                }
                catch
                {
                    info.Paid = 0.0;
                }
                info.Remark = txtRemark.Text;
                if (info.Paid < mustPaid)
                {
                    info.IsPaid = false;
                }
                else
                {
                    info.IsPaid = true;
                }
                info.IsDeleted = false;
                int checkValue = m_InvoiceCtrl.UpdateNeeded(info);
                if (checkValue > 0)
                {
                    //Refresh data grid
                    RefreshGridInvoice();
                    int rowSeletedIndex = SetSelectedForInvoiceGridView(info.InvoiceId);
                    BindDataInvoice(rowSeletedIndex);
                    MessageBoxEx.Show("Thao tác thành công !");
                }
                else
                {
                    MessageBoxEx.Show("Vui lòng thực hiện lại !");
                }
            }
            catch
            {
                MessageBoxEx.Show("Vui lòng thực hiện lại !");
            }
        }

        private void btnAddMenu_Click(object sender, EventArgs e)
        {
            try
            {
                //Add Menu to Menu List of Invoice
                InvoiceDetailInfo info = new InvoiceDetailInfo();
                info.InvoiceId = txtInvoiceId.Text;
                info.MenuId = txtMenuId.Text;
                info.State = chkState.Checked;
                info.Quantity = iiQuantity.Value;
                info.Price = Convert.ToDouble(txtPrice.Text);
                info.IsDeleted = false;
                bool isExisted = IsExistedMenu(info.InvoiceId, info.MenuId);
                if (isExisted)
                {
                    DataTable dtExisted = m_DetailCtrl.GetByMenuAndInvoice(info.InvoiceId, info.MenuId);
                    String quantity = dtExisted.Rows[0]["Quantity"].ToString();
                    double quantityDouble = Convert.ToDouble(quantity);
                    int quantityInt = Convert.ToInt32(quantityDouble);
                    info.Quantity = iiQuantity.Value + quantityInt;
                    new InvoiceDetailCtrl().Update(info);
                } 
                else
                {
                    new InvoiceDetailCtrl().Add(info);
                }
                //Update total
                double total = GetTotal(info.InvoiceId);
                InvoiceInfo infoUpdateTotal = new InvoiceInfo();
                infoUpdateTotal.InvoiceId = info.InvoiceId;
                infoUpdateTotal.Total = total;
                new InvoiceCtrl().UpdateTotal(infoUpdateTotal);
                //Refresh data grid
                RefreshGridDetail(info.InvoiceId);
                RefreshGridInvoice();
                int rowSelectedIndex = SetSelectedForInvoiceGridView(info.InvoiceId);
                BindDataInvoice(rowSelectedIndex);
            }
            catch
            {
                MessageBox.Show("Vui lòng thực hiện lại !");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //Update Menu in Menu List of Invoice
                InvoiceDetailInfo info = new InvoiceDetailInfo();
                info.InvoiceId = txtInvoiceId.Text;
                info.MenuId = txtMenuId.Text;
                info.State = chkState.Checked;
                info.Quantity = iiQuantity.Value;
                info.Price = Convert.ToDouble(txtPrice.Text);
                info.IsDeleted = false;
                new InvoiceDetailCtrl().Update(info);
                //Update total
                double total = GetTotal(info.InvoiceId);
                InvoiceInfo infoUpdateTotal = new InvoiceInfo();
                infoUpdateTotal.InvoiceId = info.InvoiceId;
                infoUpdateTotal.Total = total;
                new InvoiceCtrl().UpdateTotal(infoUpdateTotal);
                //Refresh data grid
                RefreshGridDetail(info.InvoiceId);
                RefreshGridInvoice();
                int rowSeletedIndex = SetSelectedForInvoiceGridView(info.InvoiceId);
                BindDataInvoice(rowSeletedIndex);
            }
            catch
            {
                MessageBox.Show("Vui lòng thực hiện lại !");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                //Delete Menu from Menu List of Invoice
                InvoiceDetailInfo info = new InvoiceDetailInfo();
                info.InvoiceId = txtInvoiceId.Text;
                info.MenuId = txtMenuId.Text;
                new InvoiceDetailCtrl().Delete(info);
                //Update total
                double total = GetTotal(info.InvoiceId);
                InvoiceInfo infoUpdateTotal = new InvoiceInfo();
                infoUpdateTotal.InvoiceId = info.InvoiceId;
                infoUpdateTotal.Total = total;
                new InvoiceCtrl().UpdateTotal(infoUpdateTotal);
                //Refresh data grid
                RefreshGridDetail(info.InvoiceId);
                RefreshGridInvoice();
                int rowSeletedIndex = SetSelectedForInvoiceGridView(info.InvoiceId);
                BindDataInvoice(rowSeletedIndex);
            }
            catch
            {
                MessageBox.Show("Vui lòng thực hiện lại !");
            }
        }

        private void lvCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection itemsChoise = lvCategory.SelectedItems;
            if (itemsChoise.Count > 0)
            {
                ListViewItem itemSelected = itemsChoise[0];
                ListViewItem.ListViewSubItem subId = itemSelected.SubItems[1];
                LoadMenuByCate(subId.Text);
            }
        }

        private void lvMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection itemChoise = lvMenu.SelectedItems;
            if (itemChoise.Count > 0)
            {
                ListViewItem itemSelected = itemChoise[0];
                ListViewItem.ListViewSubItem subId = itemSelected.SubItems[2];
                String menuId = subId.Text;
                txtMenuId.Text = menuId;
                DataTable dtMenuChoise = m_MenuCtrl.GetById(menuId);
                if (dtMenuChoise.Rows.Count > 0)
                {
                    String price = dtMenuChoise.Rows[0]["Price"].ToString();
                    double priceDouble = Convert.ToDouble(price);
                    String priceStr = Convert.ToString(priceDouble);
                    String unitId = dtMenuChoise.Rows[0]["UnitId"].ToString();
                    DataTable dtUnit = m_UnitCtrl.GetById(unitId);
                    String unitName = dtUnit.Rows[0]["UnitName"].ToString();
                    //bind to control
                    txtPrice.Text = priceStr;
                    lblUnit.Text = unitName;
                }
            }
        }

        private void txtDiscountPer_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double discountPer = Convert.ToDouble(txtDiscountPer.Text);
                double total = Convert.ToDouble(txtTotal.Text);
                double paidReally = total - total * discountPer / 100;
                String paidReallyStr = Convert.ToString(paidReally);
                txtPaidReally.Text = paidReallyStr;
                double debt = paidReally - Convert.ToDouble(txtPaid.Text);
                if (debt > 0)
                {
                    String debtStr = Convert.ToString(debt);
                    txtDebt.Text = debtStr;
                } 
                else
                {
                    txtDebt.Text = "0";
                }
            }
            catch
            {
                txtDiscountPer.Text = "0";
            }
        }

        private void txtPaid_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double paid = Convert.ToDouble(txtPaid.Text);
                double debt = Convert.ToDouble(txtPaidReally.Text) - paid;
                if (debt > 0)
                {
                    String debtStr = Convert.ToString(debt);
                    txtDebt.Text = debtStr;
                }
                else
                {
                    txtDebt.Text = "0";
                }
            }
            catch
            {
                txtPaid.Text = "0";
            }
        }

        private void btnExportInvoice_Click(object sender, EventArgs e)
        {
            ShowFormInvoiceDetailReport();
        }

        public void ShowFormInvoiceDetailReport()
        {
            GeneralInfo.invoiceIdReport = txtInvoiceId.Text;
            frptInvoice frm = new frptInvoice();
            frm.ShowDialog();
        }
        #endregion

        #region Change View for ListView Category
        private void btnLargeIcon_Click(object sender, EventArgs e)
        {
            lvCategory.View = System.Windows.Forms.View.LargeIcon;
        }

        private void btnDetails_Click(object sender, EventArgs e)
        {
            lvCategory.View = System.Windows.Forms.View.Details;
            lvCategory.ResetHeaderHandler();
        }

        private void btnSmallIcon_Click(object sender, EventArgs e)
        {
            lvCategory.View = System.Windows.Forms.View.SmallIcon;
        }

        private void btnList_Click(object sender, EventArgs e)
        {
            lvCategory.View = System.Windows.Forms.View.List;
        }

        private void btnTitle_Click(object sender, EventArgs e)
        {
            lvCategory.View = System.Windows.Forms.View.Tile;
        }
        #endregion

        #region Change View for ListView Menu
        private void btnLargeIcon2_Click(object sender, EventArgs e)
        {
            lvMenu.View = System.Windows.Forms.View.LargeIcon;
        }

        private void btnDetails2_Click(object sender, EventArgs e)
        {
            lvMenu.View = System.Windows.Forms.View.Details;
            lvMenu.ResetHeaderHandler();
        }

        private void btnSmallIcon2_Click(object sender, EventArgs e)
        {
            lvMenu.View = System.Windows.Forms.View.SmallIcon;
        }

        private void btnList2_Click(object sender, EventArgs e)
        {
            lvMenu.View = System.Windows.Forms.View.List;
        }

        private void btnTitle2_Click(object sender, EventArgs e)
        {
            lvMenu.View = System.Windows.Forms.View.Tile;
        }
        #endregion
    }
}
